package com.hontech.bv.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.Button
import android.widget.FrameLayout
import android.widget.TextView
import com.hontech.bv.R

class ItemView(ctx: Context, attrs: AttributeSet) : FrameLayout(ctx, attrs) {

    private val mBtn: Button
    private val mTv: TextView

    private var onClickCb: () -> Unit = {}

    init {
        val view = LayoutInflater.from(ctx).inflate(R.layout.view_item_view, this)
        mBtn = view.findViewById(R.id.id_item_view_btn)
        mTv = view.findViewById(R.id.id_item_view_tv)

        val attr = ctx.obtainStyledAttributes(attrs, R.styleable.ItemView)
        val name = attr.getText(R.styleable.ItemView_item_name)
        val ref = attr.getResourceId(R.styleable.ItemView_item_resource, R.drawable.border)
        mTv.text = name
        mBtn.setBackgroundResource(ref)
        attr.recycle()

        mBtn.setOnClickListener { onClickCb() }
        setOnClickListener { onClickCb() }
    }

    fun onClick(cb: () -> Unit) {
        onClickCb = cb
    }
}




